package com.huajx.jrb.controller;

import com.huajx.core.annotation.Route;
import com.huajx.core.handler.Controller;
import com.huajx.core.common.Columns;
import com.huajx.jrb.model.Archive;
import com.huajx.jrb.service.ArchiveService;
import com.huajx.util.StrUtil;

import java.util.Date;

@Route(controllerKey = "/archive")
public class ArchiveController extends Controller {
    public void index() {
        int ps = getParaToInt("ps", 10);
        int pi = getParaToInt("pi", 0);

        String title = getPara("title");
        String status = getPara("status");

        Columns columns = new Columns();

        if (StrUtil.isNotBlank(title)) {
            columns.like("title&code&no", title);
        }

        if (StrUtil.isNotBlank(status)) {
            columns.eq("status", status);
        }

        renderJson(ArchiveService.service.paginate(columns, pi, ps));
    }

    public void borrow(){

    }

    public void edit() {
        Long id = getParaToLong("id");
        if (id == null) {
            renderJson(new Archive());
        } else {
            renderJson(ArchiveService.service.findById(id));
        }
    }

    public void delete() {
        ArchiveService.service.delete(getParaToLong("id"));
        renderJsonSuccess();
    }

    public void save() {
        Archive archive = getModel(Archive.class);

        boolean isAdd = archive.getId() == null || archive.getId().equals(0L);
        if (isAdd) {
            archive.setCreateAt(new Date());
            ArchiveService.service.save(archive);
        } else {
            ArchiveService.service.update(archive);
        }

        try {
            ArchiveService.service.exportDoc(archive);
        } catch (Exception e) {
            e.printStackTrace();
        }
        renderJsonSuccess();
    }
}
